package com.unionftech.common.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.unionftech.baselibrary.base.BaseApplication;
import com.unionftech.common.R;

/**
 * 吐司工具类
 * 1.可以吐司图标
 * 2.toast可以在子线程中使用
 */
public class ToastUtils {
    private static final Handler mMainHandler = new Handler(Looper.getMainLooper());
    /**
     * 保证多次调用只会展示一个吐司
     */
    private static Toast mToast;
    public static final int TOAST_POSITION_CENTER = 1;
    public static final int TOAST_POSITION_DEFAULT = 2;
    public static final int TOAST_POSITION_TOP = 3;

    /**
     * 短吐司
     *
     * @param msg
     */
    public static void shortToast(String msg) {
        shortToast(BaseApplication.appContext, msg);
    }

    /**
     * 短吐司
     *
     * @param context
     * @param msg
     */
    private static void shortToast(Context context, String msg) {
        showToastChangeThread(context, msg, Toast.LENGTH_SHORT, TOAST_POSITION_CENTER);
    }

    public static void shortToast(String msg, int position) {
        showToastChangeThread(BaseApplication.appContext, msg, Toast.LENGTH_SHORT, position);
    }

    /**
     * 长吐司
     *
     * @param msg
     */
    public static void longToast(String msg) {
        longToast(BaseApplication.appContext, msg);
    }

    /**
     * 长吐司
     *
     * @param context
     * @param msg
     */
    private static void longToast(Context context, String msg) {
        showToastChangeThread(context, msg, Toast.LENGTH_LONG, TOAST_POSITION_CENTER);
    }

    public static void longToast(String msg, int position) {
        showToastChangeThread(BaseApplication.appContext, msg, Toast.LENGTH_LONG, position);
    }

    /**
     * 吐司可以在子线程中使用
     *
     * @param context
     * @param msg
     * @param duration
     */
    private static void showToastChangeThread(Context context, String msg, int duration, int position) {
        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            showToast(context, msg, duration, position);
        } else {
            mMainHandler.post(() -> showToast(context, msg, duration, position));
        }
    }

    private static void showToast(Context context, String msg, int duration, int position) {
        if (TextUtils.isEmpty(msg)) {
            return;
        }
        View toastView = null;
        if (mToast == null) {
            mToast = new Toast(context.getApplicationContext());
            toastView = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.layout_default_toast_view, null);
            mToast.setView(toastView);
        } else {
            toastView = mToast.getView();
        }
        if (toastView != null) {
            TextView textView = toastView.findViewById(R.id.toast_text);
            textView.setText(msg);
            mToast.setDuration(duration);
            setToastPosition(context.getApplicationContext(), position);
            mToast.show();
        }
    }

    private static void setToastPosition(Context context, int position) {
        int heightPixels = context.getResources().getDisplayMetrics().heightPixels;
        switch (position) {
            case TOAST_POSITION_CENTER -> mToast.setGravity(Gravity.CENTER, 0, 0);
            case TOAST_POSITION_TOP -> mToast.setGravity(Gravity.TOP, 0, heightPixels / 4);
            default -> mToast.setGravity(Gravity.BOTTOM, 0, heightPixels / 4);
        }
    }

    /**
     * 吐司图标
     *
     * @param context   上下文对象
     * @param iconResId 图标资源id
     */
    public static void showIconToast(Context context, int iconResId) {
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setGravity(Gravity.CENTER);
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        ImageView imageView = new ImageView(context);
        imageView.setImageResource(iconResId);
        linearLayout.addView(imageView, 0);

        Toast toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setView(linearLayout);
        toast.show();
    }
}
